#!/bin/bash

# PECCO Pet Shop - 一键启动所有服务

echo "======================================"
echo "启动 PECCO Pet Shop 所有服务"
echo "======================================"

# 检查是否在项目根目录
if [ ! -d "pecco_backend" ] || [ ! -d "pecco_admin_frontend" ]; then
    echo "错误: 请在项目根目录运行此脚本"
    exit 1
fi

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 检查虚拟环境
echo -e "\n${YELLOW}0. 检查环境...${NC}"
if [ ! -d "pecco_backend/venv" ]; then
    echo -e "${RED}错误: 虚拟环境不存在${NC}"
    echo "请先创建虚拟环境:"
    echo "  cd pecco_backend"
    echo "  python3 -m venv venv"
    echo "  source venv/bin/activate"
    echo "  pip install -r ../requirements.txt"
    exit 1
fi

# 检查端口是否被占用
check_port() {
    local port=$1
    if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
        return 0  # 端口被占用
    else
        return 1  # 端口空闲
    fi
}

if check_port 8000; then
    echo -e "${RED}警告: 端口 8000 已被占用${NC}"
    echo "请先停止占用该端口的进程，或使用以下命令："
    echo "  lsof -ti:8000 | xargs kill -9"
    read -p "是否继续？(y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

if check_port 5173; then
    echo -e "${RED}警告: 端口 5173 已被占用${NC}"
    echo "请先停止占用该端口的进程，或使用以下命令："
    echo "  lsof -ti:5173 | xargs kill -9"
    read -p "是否继续？(y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

echo -e "${GREEN}✓ 环境检查完成${NC}"

# 创建日志目录
if [ ! -d "logs" ]; then
    mkdir -p logs
fi

# 激活虚拟环境并检查数据库迁移
echo -e "\n${YELLOW}1. 检查数据库迁移...${NC}"
cd pecco_backend
source venv/bin/activate
python manage.py migrate
if [ $? -ne 0 ]; then
    echo -e "${RED}数据库迁移失败，请检查数据库配置${NC}"
    deactivate
    exit 1
fi
cd ..

echo -e "\n${GREEN}✓ 数据库迁移完成${NC}"

# 启动后端服务器
echo -e "\n${YELLOW}2. 启动后端服务器...${NC}"
cd pecco_backend
source venv/bin/activate
nohup python manage.py runserver > ../logs/backend.log 2>&1 &
BACKEND_PID=$!
echo -e "${GREEN}✓ 后端服务器已启动 (PID: $BACKEND_PID)${NC}"
echo "   访问: http://127.0.0.1:8000/"
echo "   日志: logs/backend.log"
cd ..

# 等待后端启动
echo "   等待后端启动..."
sleep 5

# 检查后端是否成功启动
if ! check_port 8000; then
    echo -e "${RED}错误: 后端服务器启动失败${NC}"
    echo "请查看日志: logs/backend.log"
    kill $BACKEND_PID 2>/dev/null
    exit 1
fi

# 启动前端服务器
echo -e "\n${YELLOW}3. 启动后台管理前端...${NC}"
cd pecco_admin_frontend
nohup npm run dev > ../logs/frontend.log 2>&1 &
ADMIN_PID=$!
echo -e "${GREEN}✓ 后台管理前端已启动 (PID: $ADMIN_PID)${NC}"
echo "   访问: http://localhost:5173/"
echo "   日志: logs/frontend.log"
cd ..

# 等待前端启动
echo "   等待前端启动..."
sleep 5

# 检查前端是否成功启动
if ! check_port 5173; then
    echo -e "${RED}错误: 前端服务器启动失败${NC}"
    echo "请查看日志: logs/frontend.log"
    kill $BACKEND_PID 2>/dev/null
    kill $ADMIN_PID 2>/dev/null
    exit 1
fi

echo -e "\n======================================"
echo -e "${GREEN}所有服务已启动!${NC}"
echo "======================================"
echo ""
echo "服务列表:"
echo "  - 后端 API:        http://127.0.0.1:8000/"
echo "  - Django Admin:    http://127.0.0.1:8000/django-admin/"
echo "  - 商城前端:        http://127.0.0.1:8000/ (Django 模板)"
echo "  - 后台管理前端:    http://localhost:5173/"
echo ""
echo "进程 ID:"
echo "  - 后端:            $BACKEND_PID"
echo "  - 后台管理前端:    $ADMIN_PID"
echo ""
echo "日志文件:"
echo "  - 后端日志:        logs/backend.log"
echo "  - 前端日志:        logs/frontend.log"
echo ""
echo "按 Ctrl+C 停止所有服务"
echo ""

# 创建停止函数
cleanup() {
    echo -e "\n${YELLOW}正在停止所有服务...${NC}"
    
    # 停止后端及其子进程
    if [ ! -z "$BACKEND_PID" ]; then
        pkill -P $BACKEND_PID 2>/dev/null
        kill $BACKEND_PID 2>/dev/null
    fi
    
    # 停止前端及其子进程
    if [ ! -z "$ADMIN_PID" ]; then
        pkill -P $ADMIN_PID 2>/dev/null
        kill $ADMIN_PID 2>/dev/null
    fi
    
    # 确保端口被释放
    sleep 2
    lsof -ti:8000 | xargs kill -9 2>/dev/null
    lsof -ti:5173 | xargs kill -9 2>/dev/null
    
    echo -e "${GREEN}所有服务已停止${NC}"
    exit 0
}

# 捕获 Ctrl+C
trap cleanup INT TERM

# 等待
wait

